package com.szqhyxj.youxiangju.biz.merchantHome.shop.order;

import android.app.Activity;
import android.content.ClipboardManager;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ScrollView;
import android.widget.TextView;

import com.szqhyxj.youxiangju.BaseActivity;
import com.szqhyxj.youxiangju.R;
import com.szqhyxj.youxiangju.biz.merchantHome.bean.FacilityList;
import com.szqhyxj.youxiangju.biz.merchantHome.bean.OrderDeetails;
import com.szqhyxj.youxiangju.biz.merchantHome.shop.order.view.FacilityListPopView;
import com.szqhyxj.youxiangju.sys.components.LoadImageUtils;
import com.szqhyxj.youxiangju.sys.components.NavigationBar;
import com.szqhyxj.youxiangju.sys.components.view.popview.MsgChoicePopView;
import com.szqhyxj.youxiangju.sys.util.StringUtils;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 订单详情
 */
public class OrderDetailsActivity extends BaseActivity implements OrderDetailsContract.View, NavigationBar.NavigationBarListener, MsgChoicePopView.OnClickListener{

    @BindView(R.id.navigationBar)
    NavigationBar navigationBar;
    @BindView(R.id.type_title)
    TextView typeTitle;
    @BindView(R.id.sure_receive)
    Button sureReceive;
    @BindView(R.id.type_line)
    LinearLayout typeLine;
    @BindView(R.id.information_background)
    View informationBackground;
    @BindView(R.id.icon)
    ImageView icon;
    @BindView(R.id.commodite_title)
    TextView commoditeTitle;
    @BindView(R.id.price)
    TextView price;
    @BindView(R.id.quantity)
    TextView quantity;
    @BindView(R.id.user_name)
    TextView userName;
    @BindView(R.id.user_phone)
    TextView userPhone;
    @BindView(R.id.user_name_phone)
    LinearLayout userNamePhone;
    @BindView(R.id.address)
    TextView address;
    @BindView(R.id.express_id)
    TextView expressId;
    @BindView(R.id.copy)
    TextView copy;
    @BindView(R.id.express)
    LinearLayout express;
    @BindView(R.id.express_line)
    View expressLine;
    @BindView(R.id.distribution_type)
    TextView distributionType;
    @BindView(R.id.type)
    LinearLayout type;

    @BindView(R.id.the_order_price)
    TextView theOrderPrice;
    @BindView(R.id.price_l)
    LinearLayout priceL;
    @BindView(R.id.price_line)
    View priceLine;
    @BindView(R.id.payment_type_context)
    TextView paymentTypeContext;
    @BindView(R.id.payment_type)
    LinearLayout paymentType;

    @BindView(R.id.order_id_context)
    TextView orderIdContext;
    @BindView(R.id.order_id)
    LinearLayout orderId;

    @BindView(R.id.order_time_context)
    TextView orderTimeContext;
    @BindView(R.id.order_time)
    LinearLayout orderTime;

    @BindView(R.id.delivery_time_context)
    TextView deliveryTimeContext;
    @BindView(R.id.delivery_time)
    LinearLayout deliveryTime;
    @BindView(R.id.order_details)
    ScrollView orderDetails;

    private static String ORDERID = "orderid";
    @BindView(R.id.receipt_time_context)
    TextView receiptTimeContext;
    @BindView(R.id.receipt_time)
    LinearLayout receiptTime;
    @BindView(R.id.platform_type)
    TextView platformType;
    @BindView(R.id.commodite_title_type)
    LinearLayout commoditeTitleType;
    @BindView(R.id.unit_type_context)
    TextView unitTypeContext;
    @BindView(R.id.look_unit)
    TextView lookUnit;
    @BindView(R.id.unit_type)
    LinearLayout unitType;

    private OrderDetailsPresenter presenter;

    public static void start(Activity activity, String orderid){
        Intent intent = new Intent();
        intent.setClass(activity, OrderDetailsActivity.class);
        intent.putExtra(ORDERID, orderid);
        activity.startActivity(intent);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_order_details);
        ButterKnife.bind(this);

        navigationBar.setNavigationBarListener(this);
        presenter = new OrderDetailsPresenter(this);
        String orderId = getIntent().getStringExtra(ORDERID);
        presenter.init(orderId);
    }

    @OnClick({R.id.sure_receive, R.id.copy,R.id.look_unit})
    public void onViewClicked(View view){
        switch(view.getId()){
            case R.id.sure_receive:
                MsgChoicePopView choicePopView = new MsgChoicePopView(this,"请确定是否已收到货?");
                choicePopView.setOnClickListener(this);
                choicePopView.show();
                break;
            case R.id.copy:
                showCopy(expressId.getText().toString());
                break;
            case R.id.look_unit:
                presenter.initList();
                break;
        }
    }

    @Override
    public void showOrderDetails(OrderDeetails deetails){
        orderDetails.setVisibility(View.VISIBLE);

        String typeTxt;
        if(deetails.getByType().equals("2")){  //1=上级发货，2=平台发货
            platformType.setText(getResources().getString(R.string.platform_delivery));
            distributionType.setText("物流配送");
            typeTxt = "平台";
        }else{
            platformType.setText(getResources().getString(R.string.superior_delivery));
            typeTxt = "上级";
        }

        if(deetails.getDeliverStatus().equals("0")){  //未发货
            typeTitle.setText("支付成功,等待"+typeTxt+"发货");
            sureReceive.setVisibility(View.GONE);
            express.setVisibility(View.GONE);
            receiptTime.setVisibility(View.GONE);
            deliveryTime.setVisibility(View.GONE);
            lookUnit.setVisibility(View.GONE);
        }else if(deetails.getReceiverStatus().equals("0")){//未收货
            typeTitle.setText(typeTxt+"已发货");
            receiptTime.setVisibility(View.GONE);
        }else if(deetails.getReceiverStatus().equals("1")){ //已收货
            typeTitle.setText("已确定收货,订单完成");
            sureReceive.setVisibility(View.GONE);
        }
        LoadImageUtils.loadImage(icon, deetails.getFile());
        commoditeTitle.setText(deetails.getPackageName());

        price.setText(StringUtils.initPrice(deetails.getTotalPrice()));
        quantity.setText("/" + deetails.getTotalNum() + "台");
        userName.setText(deetails.getUsername());
        userPhone.setText(deetails.getPhone());
        address.setText(deetails.getAddress());
        expressId.setText(deetails.getTrackingNumber());
        unitTypeContext.setText(deetails.getBrandSubName());

        if(TextUtils.isEmpty(deetails.getDistributionType())||deetails.getByType().equals("1")){
            type.setVisibility(View.GONE);
        }else {
            if(deetails.getDistributionType().equals("1")){ //1=当面配送，2=物流配送
                distributionType.setText("当面配送");
            }else{
                distributionType.setText("物流配送");
            }
        }

        theOrderPrice.setText(StringUtils.initPrice(deetails.getTotalPrice())+"元");

        orderIdContext.setText(deetails.getPayOrderId());
        orderTimeContext.setText(deetails.getAddTime());
        deliveryTimeContext.setText(deetails.getDeliverTime());
        receiptTimeContext.setText(deetails.getDeliverTime());
    }

    @Override
    public void showConfirmOrder(){
        typeTitle.setText("已确定收货,订单完成");
        sureReceive.setVisibility(View.GONE);
    }

    @Override
    public void showCopy(String context){
        ClipboardManager cm = (ClipboardManager) getSystemService(Context.CLIPBOARD_SERVICE);
        // 将文本内容放到系统剪贴板里。
        cm.setText(context);
        showToast("复制成功");
    }

    @Override
    public void showListPop(List<FacilityList> facilityList) {
        FacilityListPopView popView = new FacilityListPopView(this, facilityList);
        popView.setOnClickListener(new FacilityListPopView.OnClickListener() {
            @Override
            public void onClickConfirm(View v) {

            }

            @Override
            public void close(View v) {
                popView.dismiss();
            }

            @Override
            public void onCancel(View v) {

            }
        });
        popView.show();
    }



    @Override
    public void navigationLeft(){
        finish();
    }

    @Override
    public void navigationRight(){
    }

    @Override
    public void onClickConfirm(View v){
        presenter.nowConfirm();
    }
}
